package com.hbnu.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.core.util.PageUtil;
import com.hbnu.system.mapper.KnowleageMapper;
import com.hbnu.system.model.dto.SearchPageDTO;
import com.hbnu.system.model.entity.Knowleage;
import com.hbnu.system.service.IKnowleageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@Service
public class KnowleageServiceImpl extends ServiceImpl<KnowleageMapper, Knowleage> implements IKnowleageService {

    @Autowired private IKnowleageService iKnowleageService;

    @Override
    public boolean addAppliMaterials(Knowleage knowLeage) {
        if(StrUtil.isEmpty(knowLeage.getName()) || StrUtil.isBlank(knowLeage.getName())){
            throw new BaseException("请输入名称");
        }

        Knowleage one = super.getOne(Wrappers.lambdaQuery(Knowleage.class)
                .eq(Knowleage::getName, knowLeage.getName()));
        if(Objects.nonNull(one)){
            throw new BaseException("该名称已被使用!");
        }

        if(StrUtil.isEmpty(knowLeage.getSystemID()) || StrUtil.isBlank(knowLeage.getSystemID())){
            throw new BaseException("请输入系统编号");
        }

//        if(StrUtil.isEmpty(knowLeage.getAnnex().toString()) || StrUtil.isBlank(knowLeage.getAnnex().toString())){
//            throw new BaseException("请选择附件");
//        }

        LocalDate nowDate = LocalDate.now();

        if(StrUtil.isEmpty(knowLeage.getYear()) || StrUtil.isBlank(knowLeage.getYear())){
            throw new BaseException("请输入所属年度");
        }
        String substring = nowDate.toString().substring(0, 4);
        if(Integer.parseInt(knowLeage.getYear()) > Integer.parseInt(substring)){
            throw new BaseException("所属年度有误!");
        }
        if(Objects.isNull(knowLeage.getEnteredDate())){
            knowLeage.setEnteredDate(LocalDate.now());
        }

        return baseMapper.insert(knowLeage) == 1;

    }

    @Override
    public boolean deleteAppliMaterials(String appliMaterialsID) {
        Knowleage one = super.getOne(Wrappers.lambdaQuery(Knowleage.class)
                .eq(Knowleage::getAppliMaterialsID, appliMaterialsID));
        if(Objects.isNull(one)){
            throw new BaseException("申报材料编号有误");
        }
        return baseMapper.deleteById(appliMaterialsID) == 1;

    }

    @Override
    public boolean updateAppliMaterials(Knowleage knowleage) {
        Knowleage one = super.getOne(Wrappers.lambdaQuery(Knowleage.class)
                .eq(Knowleage::getAppliMaterialsID, knowleage.getAppliMaterialsID()));
        if (Objects.isNull(one)){
            throw new BaseException("申报材料编号有误");
        }

        if(StrUtil.isEmpty(knowleage.getName()) || StrUtil.isBlank(knowleage.getName())){
            throw new BaseException("名称不可为空");
        }

        if(StrUtil.isEmpty(knowleage.getSystemID()) || StrUtil.isBlank(knowleage.getSystemID())){
            throw new BaseException("系统编号不可为空");
        }

//        if(StrUtil.isEmpty(knowLeage.getAnnex().toString()) || StrUtil.isBlank(knowLeage.getAnnex().toString())){
//            throw new BaseException("附件不可为空");
//        }

        LocalDate nowDate = LocalDate.now();

        if(StrUtil.isEmpty(knowleage.getYear()) || StrUtil.isBlank(knowleage.getYear())){
            throw new BaseException("所属年度不可为空");
        }
        String substring = nowDate.toString().substring(0, 4);
        if(Integer.parseInt(knowleage.getYear()) > Integer.parseInt(substring)){
            throw new BaseException("所属年度有误!");
        }
//        knowleage.setEnteredDate(nowDate);

        return baseMapper.updateById(knowleage) == 1;
    }

    @Override
    public Knowleage getAppliMaterials(String appliMaterialsID) {
        Knowleage knowleage = super.getOne(Wrappers.lambdaQuery(Knowleage.class)
                .eq(Knowleage::getAppliMaterialsID, appliMaterialsID));
        if(Objects.isNull(knowleage)){
            throw new BaseException("申报材料编号异常!未查找到该申报材料");
        }
        return knowleage;
    }

    @Override
    public IPage<Knowleage> pageCondition(SearchPageDTO searchPageDTO) {

        IPage<Knowleage> page = PageUtil.getPage(searchPageDTO.getPageNum(), searchPageDTO.getPageSize());
        LambdaQueryWrapper<Knowleage> wrapper = Wrappers.lambdaQuery(Knowleage.class);
        // 检查 projectName 是否为空或空白
        if (StrUtil.isNotBlank(searchPageDTO.getSearch())) {
            // 如果 projectName 不为空或空白，添加模糊查询条件
            wrapper.like(Knowleage::getName, searchPageDTO.getSearch());
        }
        IPage<Knowleage> knowleageIPage = baseMapper.selectPage(page, wrapper);

        List<Knowleage> records = knowleageIPage.getRecords();
        List<Knowleage> KnowleageS = new ArrayList<>();

        int count = 1;
        int start = searchPageDTO.getPageSize() * (searchPageDTO.getPageNum()-1)+1;
        int end = searchPageDTO.getPageNum() * searchPageDTO.getPageSize();
        Collections.reverse(records);
        for (Knowleage record:records) {
            if (count >= start && count <= end){
                KnowleageS.add(this.getAppliMaterials(record.getAppliMaterialsID()));
            }
            count += 1;
        }

//        Collections.reverse(KnowleageS);
        knowleageIPage.setRecords(KnowleageS);
        knowleageIPage.setTotal(records.size());
        return knowleageIPage;
    }
}
